package com.example.noelwhereismystuffs.models;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class BackEndDatabase {
	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "user_username";
	public static final String KEY_EMAIL = "user_email";
	public static final String KEY_PASSWORD = "user_password";
	public static final String KEY_TYPE = "user_type";
	public static final String KEY_LOGIN_ATTEMP = "user_login_attemp";
	public static final String KEY_STATUS = "user_status"; // locked or not

	private static final String DATABASE_NAME = "BackEndDb";
	private static final String DATABASE_TABLE = "userTable";
	private static final int DATABASE_VERSION = 1;

	private DbHelper myHelper;
	private final Context myContext;
	private SQLiteDatabase myDatabase;

	private static class DbHelper extends SQLiteOpenHelper {

		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
					+ " TEXT NOT NULL, " + KEY_EMAIL + " TEXT NOT NULL, "
					+ KEY_PASSWORD + " TEXT NOT NULL, " + KEY_TYPE
					+ " TEXT NOT NULL, " + KEY_LOGIN_ATTEMP
					+ " TEXT NOT NULL, " + KEY_STATUS + " TEXT NOT NULL);");

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

	}

	public BackEndDatabase(Context c) {
		myContext = c;
	}

	public BackEndDatabase open() {
		myHelper = new DbHelper(myContext);
		myDatabase = myHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		myHelper.close();
	}

	/**
	 * Add new User to the database
	 * 
	 * @param username
	 * @param email
	 * @param password
	 */
	public long addUser(String username, String email, String password) {
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, username);
		values.put(KEY_EMAIL, email);
		values.put(KEY_PASSWORD, password);
		values.put(KEY_LOGIN_ATTEMP, "3");
		values.put(KEY_STATUS, "unlocked");
		values.put(KEY_LOGIN_ATTEMP, "3");
		values.put(KEY_TYPE, "user");
		return myDatabase.insert(DATABASE_TABLE, null, values);
	}

	public String[] getUser(String email) {
		String[] columns = new String[] { KEY_ROWID, KEY_EMAIL, KEY_PASSWORD };
		Cursor c = myDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String res = "";
		int iRow = c.getColumnIndex(KEY_ROWID);
		int iEmail = c.getColumnIndex(KEY_EMAIL);
		int iPassword = c.getColumnIndex(KEY_PASSWORD);
		
		for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
			res = res + c.getString(iRow) + " " + c.getString(iEmail) + " " + c.getString(iPassword) + "\n" ;
		}
		
		return null;
	}

}
